<script setup lang="ts">
import { ref, inject } from 'vue'
import { useRouter } from 'vue-router'
import { 
  BorderBox12 as DvBorderBox12, 
  BorderBox5 as DvBorderBox5,
  Decoration5 as DvDecoration5 
} from '@kjgl77/datav-vue3'
import { useSensorStore } from '@/stores/sensor'

const router = useRouter()
const props = defineProps({
  overrideDataTraceText: { type: String, default: '数据回溯' },
  onDataTraceClick: { type: Function },
  overrideRefreshFunction: { type: Function },
  overrideRefreshText: { type: String, default: '刷新数据' }
})

const sensorStore = useSensorStore()

// 处理点击事件
const handleClick = () => {
  if (props.onDataTraceClick) {
    props.onDataTraceClick()
  } else {
    router.push('/dataTrace')
  }
}

const currentTime = ref('')
const currentDate = ref('')

// 更新时间
const updateTime = () => {
  const now = new Date()
  const days = ['日', '一', '二', '三', '四', '五', '六']
  
  // 格式化时间
  currentTime.value = now.toLocaleTimeString('zh-CN', {
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: false
  })
  
  // 格式化日期
  currentDate.value = `${now.getFullYear()}/${String(now.getMonth() + 1).padStart(2, '0')}/${String(now.getDate()).padStart(2, '0')} 周${days[now.getDay()]}`
}

// 启动定时器
setInterval(updateTime, 1000)
updateTime() // 立即执行一次

const logout = () => {
  localStorage.removeItem('isLogin')  // 清除登录状态
  router.push('/login')  // 跳转到登录页
}

// 修改刷新处理函数
const handleRefresh = async () => {
  if (props.overrideRefreshFunction) {
    await props.overrideRefreshFunction()
  } else {
    await sensorStore.fetchLatestData()
    await sensorStore.fetchWeekData()
  }
}

</script>

<template>
  <div class="top-panel">
    <div class="content-wrapper">
      <!-- 左侧按钮 -->
      <div class="left-section">
        <dv-border-box12 class="button-box">
          <button class="nav-button" @click="handleRefresh">
            <span class="button-text">
              {{ props.overrideRefreshText }}
            </span>
          </button>
        </dv-border-box12>
        <dv-border-box12 class="button-box">
          <button class="nav-button" @click="handleClick">
            <span class="button-text">
              {{ props.overrideDataTraceText }}
            </span>
          </button>
        </dv-border-box12>
        <dv-border-box12 class="button-box">
          <div class="button-box">
            <button class="nav-button" @click="logout">
              退出登录
            </button>
          </div>
        </dv-border-box12>
      </div>
      
      <!-- 中间标题 -->
      <div class="title-wrapper">
        <dv-border-box5 :reverse="true"
          class="title-box"
          :color="['#4fd2dd', '#235fa7']"
        >
          <h1 class="main-title">智慧农场实时监控系统</h1>
        </dv-border-box5>
      </div>

      <!-- 右侧时间显示 -->
      <div class="right-section">
        <dv-border-box12 class="place-box">
          <div class="place-wrapper">
            <span class="place">二号智慧大棚</span>
          </div>
        </dv-border-box12>
        
        <dv-border-box12 class="datetime-box">
          <div class="datetime-wrapper">
            <span class="date">{{ currentDate }}</span>
            <span class="time">{{ currentTime }}</span>
          </div>
        </dv-border-box12>
      </div>
    </div>
  </div>
  
  <div class="decoration-wrapper">
    <dv-decoration5 :dur="2" />
  </div>
</template>

<style scoped>
.top-panel {
  width: 2560px;
  height: 100px;
  position: relative;
  background-color: rgba(0, 24, 48, 0.6);
}

/* 修改装饰容器样式 */
.decoration-wrapper {
  width: 2560px;
  height: 20px;
}

.content-wrapper {
  width: 100%;
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 40px;
}

.left-section {
  display: flex;
  gap: 80px;
  margin-left: 200px;
  margin-top: 50px;
}

.button-box {
  width: 160px;
  height: 50px;
}

.nav-button {
  width: 100%;
  height: 100%;
  font-size: 22px;
  color: #d9faff;
  background: transparent;
  border: none;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0;
  outline: none;
}

.nav-button:hover {
  color: #64ffda;
}

/* 添加按钮点击效果 */
.nav-button:active {
  transform: scale(0.98);
}

.title-wrapper {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 10;
}

.title-box {
  width: 500px;
  height: 90px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 10px;
  margin-left: 10px;
}

.main-title {
  font-size: 30px;
  font-weight: bold;
  background: linear-gradient(135deg,#6199f3 0%, #64ffac 100% );
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  white-space: nowrap;
  margin-top: 20px;
  padding-left: 120px;
}

.right-section {
  display: flex;
  align-items: center;
  margin-right: 10px;
  margin-top: 50px;
  gap: 100px;
}

.place-box {
  width: 250px;
  height: 60px;
  color: #d9faff;
}

.place-wrapper {
  color: #d9faff;
  font-size: 25px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.datetime-box {
  width: 300px;
  height: 60px;
}

.datetime-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 4px;
}

.time {
  color: white;
  font-size: 20px;
  font-weight: bold;
}

.date {
  color: white;
  font-size: 15px;
}

/* 添加按钮悬停下划线效果 */
.button-text::after {
  content: '';
  position: absolute;
  width: 0;
  height: 2px;
  bottom: -2px;
  left: 50%;
  background-color: #64ffda;
  transition: all 0.3s ease;
}

.nav-button:hover .button-text::after {
  width: 100%;
  left: 0;
}

:deep(.el-button--primary) {
  background-color: #00e4ff;
  border-color: #00e4ff;
}

:deep(.el-button--primary:hover) {
  background-color: #33eaff;
  border-color: #33eaff;
}

:deep(.el-button--primary:active) {
  background-color: #00b4cc;
  border-color: #00b4cc;
}

</style>
